//index.js
//获取应用实例
var app = getApp()
var admin_id = app.globalData.admin_id;
var cat_id = 4;
var share = app.globalData.share;
var timer = null;
Page({
	data: {
		isroll: true,
		curindex: 4,
		popshow: false,
		msg: '敬请期待！',
    canIUse: false
	},
	onShow: function () {
		var that = this;
    wx.getSetting({
      success: function (res) {
        console.log('show',res)
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          that.setData({
            canIUse: false
          })
        }else{
          that.setData({
            canIUse: true
          })
        }
      }
    })
		if (app.globalData.sw_vip == 1 || app.globalData.sw_int == 1){
			app.checkUser(app.globalData.openid, that, function () {
				wx.reLaunch({
					url: '../../pages/reg/reg',
				})
			})
		}
	},
  bindGetUserInfo:function(e){
    console.log('userInfo', e.detail.userInfo)
    app.globalData.userInfo = e.detail.userInfo;
    this.setData({
      userInfo: e.detail.userInfo,
      canIUse: false
    })
  },
	onLoad: function () {
		var that = this;
    
    
		that.setData({
			userInfo: app.globalData.userInfo,
			sw_int: app.globalData.sw_int,
			sw_vip: app.globalData.sw_vip,
		})
	},
	filterHeaderCatid: function (e) {
		return (e.cat_id == cat_id)
	},
	goodsClick: function (e) {
		console.log(e)
		wx.navigateTo({
			url: '../../pages/details/details?id=' + e.currentTarget.dataset.id,
		})
	},
	onPageScroll: function (e) {
		// console.log(e)
		var that = this;
		clearTimeout(timer)
		that.setData({
			isroll: false
		})
		timer = setTimeout(function () {
			that.setData({
				isroll: true
			})
		}, 800)
	},
	getInt: function () {
		var that = this;
		wx.request({
			url: app.globalData.host + 'index.php/Api/user/score?openid=' + app.globalData.openid,
			success: function (e) {
				console.log(e)
				that.setData({
					int: e.data.score
				})
			}
		})
	},
	jifeng: function () {
		this.setData({
			msg: '敬请期待！',
			popshow: true
		})
	},
	qiandao: function () {
		var that = this;
		if (this.sign()) {
			wx.showLoading({
				title: '签到中..',
			})
			app.opInt(2, 'integral', 1, function () {
				app.checkUser(app.globalData.openid, that)
				app.showPop('积分 +2', true)
			}, function () {
				// wx.hideLoading()
			})

		} else {
			app.showPop('今日已签', false)
		}

	},
	sign: function (num) {
		//wx.removeStorageSync('gru_sign');return;
		//返回是否可以签到
		//num 每天签到的次数
		if (!num) {
			num = 1
		}
		var myDate = new Date();
		myDate.getFullYear(); //获取完整的年份(4位,1970-????)
		myDate.getMonth(); //获取当前月份(0-11,0代表1月)
		myDate.getDate(); //获取当前日(1-31)
		var date = myDate.getFullYear() + '-' + myDate.getMonth() + '-' + myDate.getDate();
		//读取缓存
		var value = wx.getStorageSync('gru_sign')
		//首次没读到 设置指并 存入 return true
		if (!value) {
			value = {
				date: date,
				times: 1
			};
			wx.setStorageSync('gru_sign', value);
			return true;
		}
		/*有值
		判断日期
		*/
		if (value.date == date) {
			if (value.times >= num) {
				return false
			} else {
				value.times++
			}
		} else {
			value.date = date;
			value.times = 1;
		}
		wx.setStorageSync('gru_sign', value);
		return true;
	},
	tobuy: function () {
		wx.navigateTo({
			url: '../../pages/buy/buy',
		})
	},
	toorder:function(){
		wx.navigateTo({
			url: '../../pages/orders/orders',
		})	
	},
  toyuyeorder(){
    wx.navigateTo({
      url: '../../pages/yuyeorder/yuyeorder',
    })
  },
  toc(){
    wx.navigateTo({
      url: '../../pages/myc/myc',
    })	
  },
	toOOrder:function(e){
		wx.navigateTo({
			url: '../../pages/orders/orders?id=' + e.currentTarget.dataset.id,
		})
	},
	choseaddress:function(){
		wx.chooseAddress({
			
		})
	},
  toyouhui(){
    wx.navigateTo({
      url: '../../pages/youhui/youhui',
    })
  },
  tovip(){
    wx.navigateTo({
      url: '../../pages/vipcd/vipcd',
    })
  }
})
